/*
* @Author: Administrator
* @Date:   2018-01-13 11:33:55
* @Last Modified by:   Administrator
* @Last Modified time: 2018-01-15 09:58:56
*/
import axios from 'axios'
import { restApi } from '../constant/api'

function obj2params(obj) {
  let result = ''
  Object.keys(obj).forEach((key) => {
    result += `&${key}=${encodeURIComponent(obj[key])}`;
  })

  return result.substr(1)
}
const host = process.env.API_BASE || ''
// get请求
export function get(baseUrl, params, headersParam) {
  const url = `${host}${restApi}${baseUrl}?${obj2params(params)}`;
  const headers = Object.assign({}, {
    Accept: 'application/json, text/plain, */*',
  }, headersParam);
  const result = axios({
    url,
    headers,
  })
  return result;
}
export function post(baseUrl, params, headersParam) {
  const url = `${host}${restApi}${baseUrl}`;
  const headers = Object.assign({}, {
    Accept: 'application/json, text/plain, */*',
  }, headersParam);
  const result = axios({
    method: 'post',
    url,
    headers,
    data: params,
  })
  return result;
}
